<script>
import { getVideo } from "../assets/api/info.js";
import { onMounted, reactive, toRefs } from "vue";
import { useRouter } from "vue-router";
export default {
  setup() {
    let router = useRouter();

    const list = reactive({
      bannerList: [],
      classList: [],
      videoList: [],
      videoLists: [],
      loading: false,
      finished: false,
      index: 0,
      editId: 1,
    });
    const goClassVideo = (item) => {
      router.push({
        path: "/videoclass",
        query: {
          id: item.id,
          name: item.zh_name,
        },
      });
    };

    // 过滤器
    function filters(id) {
      let w = id > 10000 ? (id / 10000).toFixed(1) + "万" : id;
      return w;
    }
    const details_article = (data, i, index) => {
      // data = JSON.stringify(data);
      let id = (i + 1) * (index + 1);
      window.localStorage.setItem("article_video", JSON.stringify(data));
      router.push({
        path: "/detailsvideo",
        query: {
          id,
        },
      });
    };
    const onLoad = () => {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        for (let i = 0; i < 1; i++) {
          list.index += 1;
          list.videoList = list.videoLists.slice(0, list.index);
        }

        // 加载状态结束
        list.loading = false;

        // 数据全部加载完成
        if (list.videoList.length >= 5) {
          list.finished = true;
        }
      }, 2000);
    };
    onMounted(() => {
      list.editId = window.localStorage.getItem("editId");

      //视频
      getVideo().then((data) => {
        console.log(data);
        list.bannerList = data.data.banner;
        list.classList = data.data.type_menu;
        list.videoList = data.data.video_list.slice(1, 2);
        list.videoLists = data.data.video_list.slice(1, 6);
      });
    });
    return {
      ...toRefs(list),
      filters,
      onLoad,
      goClassVideo,
      details_article,
    };
  },
};
</script>
<template>
  <div class="container">
    <van-sticky>
      <top-coms v-if="editId == 2"></top-coms>
      <top-com v-if="editId == 1"></top-com>
      <div class="tabs" v-if="editId == 1">
        <ul>
          <router-link custom to="/info" v-slot="{ href, navigate }">
            <li :href="href" @click="navigate">热门</li>
          </router-link>
          <router-link custom to="/infonew" v-slot="{ href, navigate }">
            <li :href="href" @click="navigate">资讯</li>
          </router-link>
          <router-link custom to="/infovideo" v-slot="{ href, navigate }">
            <li :href="href" @click="navigate" class="con">视频</li>
          </router-link>
        </ul>
      </div>
      <div class="tabse" v-if="editId == 2">
        <ul>
          <router-link custom to="/info" v-slot="{ href, navigate }">
            <li :href="href" @click="navigate">热门</li>
          </router-link>
          <router-link custom to="/infonew" v-slot="{ href, navigate }">
            <li :href="href" @click="navigate">资讯</li>
          </router-link>
          <router-link custom to="/infovideo" v-slot="{ href, navigate }">
            <li :href="href" @click="navigate" class="con">视频</li>
          </router-link>
        </ul>
      </div>
    </van-sticky>

    <div :class="['banner', { backg: editId == 1 }]">
      <div class="bg" v-if="editId == 1">
        <img src="../assets/img/lbtbg.png" alt="" />
      </div>
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="item in bannerList" :key="item.id">
          <img :src="item.app_icon" alt="" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <div :class="['classTab', { classTabs: editId == 2 }]">
      <ul>
        <li
          v-for="item in classList"
          :key="item.id"
          @click="goClassVideo(item)"
        >
          <img :src="item.app_small_icon" alt="" />
          <div class="name">{{ item.zh_name }}</div>
        </li>
      </ul>
    </div>
    <div :class="['pd', { pds: editId == 2 }]">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div
          class="hot"
          v-for="(item, index) in videoList"
          :key="item.category_id"
        >
          <div class="hot-title">
            <span>{{ item.category.category_name }}</span>
            <a href="javascript:;" class="more">
              <div class="title">更多</div>
              <i class="iconfont icon-xiayibu"></i>
            </a>
          </div>

          <ul class="hotlist">
            <li
              v-for="(items, i) in item.category.list"
              :key="items.id"
              @click="details_article(items, i, index)"
            >
              <div class="img">
                <img :src="items.video_icon" alt="" />
                <div class="text">
                  <div class="num">
                    <i class="iconfont icon-paishipin"></i>
                    <span>{{ filters(items.click_num) }}</span>
                  </div>
                  <div class="time">
                    <span>{{ items.video_format_time }}</span>
                  </div>
                </div>
              </div>
              <div class="bottom">
                <div class="title one-txt-cut">{{ items.title }}</div>
                <div :class="['label',{labels:editId == 2}]">{{ item.category.category_name }}</div>
              </div>
            </li>
          </ul>
        </div>
      </van-list>
    </div>

    <bottom-com :tabId="(tabId = 2)" v-if="editId == 1"></bottom-com>
    <bottom-coms :tabId="(tabId = 2)" v-if="editId == 2"></bottom-coms>
  </div>
</template>
<style lang="scss" scoped>
// 分类
.classTab {
  padding: 30px 10px 10px;
  ul {
    display: flex;
    overflow-y: hidden;
    overflow-x: auto;
    &::-webkit-scrollbar {
      display: none;
    }
    li {
      display: flex;
      align-items: center;
      flex-direction: column;
      margin-right: 15px;
      text-align: center;
      .name {
        width: 52px;
        color: #929292;
        font-size: 10px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      img {
        margin-bottom: 8px;
        width: 52px;
        height: 42px;
      }
    }
  }
}
.classTabs {
  padding: 5px 10px 10px;
}
// 视频

.hot {
  padding: 0px 10px 0;
  .hot-title {
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    color: #242629;
    font-weight: 700;
    display: flex;
    justify-content: space-between;
    .more {
      display: flex;
      color: #888;

      .title {
        font-size: 12px;
      }
      .iconfont {
        margin-left: 4px;
        font-size: 12px;
      }
    }
  }
  h3 {
    font-size: 16px;
    font-weight: 600;
    font-family: 黑体;
    margin: 10px 0 10px;
  }
  ul {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    li {
      width: 49%;
      background-color: #fff;
      border-radius: 8px;
      margin-bottom: 10px;
      .img {
        position: relative;
        // width: 100%;
        img {
          width: 100%;
          height: 98px;
          border-radius: 8px 8px 0 0;
        }
        .text {
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 36px;
          background: linear-gradient(
            -180deg,
            rgba(0, 0, 0, 0) 0%,
            #000000 97%
          );
          z-index: 1;
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 16px 8px 0;
          line-height: 36px;
          .num {
            height: 17px;
            line-height: 17px;
            font-size: 10px;
            text-align: right;
            display: flex;
            align-items: center;
            .iconfont {
              color: #fff;
              font-size: 10px;
              margin-right: 4px;
            }
            span {
              font-size: 10px;
              color: #fff;
            }
          }
          .time {
            height: 17px;
            line-height: 17px;
            font-size: 10px;
            text-align: right;
            color: #fff;
          }
        }
      }
      .bottom {
        height: 40px;
        display: flex;
        align-items: center;
        .title {
          flex: 1;
          color: #1e1e1e;
          font-size: 13px;
          transform: scale(0.85, 0.85);
        }

        .label {
          color: #fe1f73;
          font-size: 14px;
          transform: scale(0.83, 0.83);
          padding: 0 4px;
          height: 24px;
          line-height: 24px;
          border-radius: 6px 3px 5px 6px;
          background-color: #ffd3df;
          text-align: center;
          margin-right: 8px;
        }
        .labels {
          color: #6a3aed;
          background-color: #f2ecf9;
        }
      }
    }
  }
}
// 轮播图
.bg {
  position: absolute;
  width: 100%;
  left: 0;
  right: 0;
  margin: 0 auto;
  max-width: 750px;
  > img {
    width: 100%;
    height: 45vw;
  }
}
.backg {
  background: linear-gradient(270deg, #660234 0%, #7b0b3b 100%);
}
.banner {
  padding: 10px;
  width: 100%;

  > img {
    position: relative;
    width: 100%;
    // height: 39vw;
    border-radius: 5px;
  }
  .my-swipe {
    border-radius: 10px;
    .van-swipe-item {
      color: #fff;
      font-size: 20px;
      line-height: 150px;
      text-align: center;
      background-color: #39a9ed;
      img {
        width: 100%;
        height: 138px;
      }
    }
  }
}
.bannerlist {
  margin-top: 15px;
  padding: 5px 10px 10px;
  .van-swipe {
    .van-swipe-item {
      background-color: #f9f8fd;
      height: 100px;
      border-radius: 5px;
      .items {
        margin-right: 10px;
        padding: 5px 11px;
        background-color: #fff;
        height: 100px;
        border-radius: 5px;
        &::after {
          content: "";
          position: absolute;
          top: 1px;
          left: 0;
          height: 2px;
          width: 93%;
          border-radius: 2px 2px 0 0;
          background: linear-gradient(270deg, #7b0b3b 0%, #d51c78 100%);
        }

        .top {
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 14px;
          color: #888888;
          margin-bottom: 6px;
          height: 28px;
        }
        .content {
          display: flex;
          align-items: center;
          justify-content: space-between;
          .left {
            .item {
              display: flex;
              align-items: center;
              font-family: 黑体;
              font-weight: 600;
              font-size: 12px;
              &:nth-child(1) {
                margin-bottom: 4px;
              }
              // .name{
              //   // width: 84px;
              // }
              .img {
                width: 24px;
                height: 24px;
                margin-right: 8px;
                img {
                  width: 24px;
                  height: 24px;
                }
              }
            }
          }
          .iconfont {
            font-size: 22px;
            color: #888888;
          }
        }
      }
    }
  }
}
.tabs {
  background: linear-gradient(270deg, #660234 0%, #7b0b3b 100%);
  overflow-y: hidden;
  overflow-x: auto;
  display: flex;
  &::-webkit-scrollbar {
    display: none;
  }
  ul {
    height: 37px;
    padding-top: 4px;
    line-height: 38px;
    position: relative;

    // margin: 0 auto;
    white-space: nowrap;
    display: flex;
    position: relative;
    margin-bottom: 6px;

    li {
      position: relative;
      color: #d679a6;
      font-size: 16px;
      margin: 0 18px;

      &.active {
        //  font-size: 20px;
        font-weight: 700;
        &::before {
          content: "";
          position: absolute;
          top: auto;
          left: 18%;
          bottom: -6px;
          right: auto;
          height: 3px;
          width: 60%;
          background: linear-gradient(270deg, #8e4ff5 0%, #7172f7 100%);
          border-radius: 2px;
        }
      }
    }
    .con {
      font-size: 16px;
      color: #f2cc00;
      font-weight: 600;
      position: relative;
    }
  }
}
.pd {
  padding-bottom: 95px;
}
.pds {
  padding-bottom: 60px;
}
.tabse {
  width: 100%;
  background-color: #fff;
  border-bottom: 1px solid #ededef;
  overflow-y: hidden;
  overflow-x: auto;
  display: flex;
  &::-webkit-scrollbar {
    display: none;
  }
  ul {
    height: 37px;
    padding-top: 4px;
    line-height: 38px;
    position: relative;

    // margin: 0 auto;
    white-space: nowrap;
    display: flex;
    position: relative;
    margin-bottom: 6px;

    li {
      position: relative;
      color: #3f3f3f;
      font-size: 16px;
      margin: 0 18px;

      &.con {
        //  font-size: 20px;
        font-weight: 700;
        &::before {
          content: "";
          position: absolute;
          top: auto;
          left: 18%;
          bottom: -6px;
          right: auto;
          height: 3px;
          width: 60%;
          background: linear-gradient(270deg, #8e4ff5 0%, #7172f7 100%);
          border-radius: 2px;
        }
      }
    }
  }
}
</style>
